/**
 * Created by hjqi on 2016/5/5.
 */

var config = require('../config/config').config;
var httpRequest = require('../common/httpRequest').httpRequest;
var _req = new httpRequest();
var qs = require('querystring');
var logger = require('../log4js/log4js').logger('jenkins.js');
var util = require('util');

module.exports = libJenkins;

//opBuild Info part will move to this

function libJenkins(){

}

libJenkins.prototype.createView = function(username){
    return new Promise(function (resolve, reject) {
        var _url = 'http://'+config.jenkins.host+':'+config.jenkins.port+'/createView';
        var json = {
            "name":username,
            "mode":"hudson.model.ListView"
        };
        var _data = {
            "name":username,
            "mode":"hudson.model.ListView",
            "json":JSON.stringify(json),
            "Submit":"OK"
        };
        var _postData  = qs.stringify(_data);

        logger.debug("Jenkins create view url: "+_url ) ;
        //create a view in jenkins(view just like namespace that distinguish from different users)
            _req._request(_url, 'POST',{"Content-Type":"application/x-www-form-urlencoded","Content-Length":_postData.length} , _postData  , function (data, result, response) {

                //sometime response will be undefined
                logger.debug("Jenkins create view response: "+(response===undefined?'undefined':response.statusCode)) ;
                logger.debug("Jenkins create view result: "+JSON.stringify( result ) ) ;

                if(response !== undefined){

                    if (response.statusCode === 302 ){
                        resolve(true);
                    } else{
                        //filter exists error when create
                        if(result.indexOf('already exists') !== -1) {
                            logger.error(_url + ":" + _data.name + " already existed");
                            resolve(true);
                        } else {
                            logger.error("[NoView]"+_url + ":" + util.inspect(result, {showHidden: false, depth: null}));
                            reject(false);
                        }
                    }
                }else{
                    logger.error("[NoResponse]"+_url + ":" + util.inspect(data, {showHidden: false, depth: null}));
                    reject(false);
                }
            });//-

    });
}

libJenkins.prototype.deleteView = function(username){
    return new Promise(function (resolve, reject) {
        var _url = 'http://'+config.jenkins.host+':'+config.jenkins.port+'/view/'+username+'/doDelete';
        var json = {};
        var _data = {
            "json":JSON.stringify(json),
            "Submit":"Yes"
        };
        var _postData  = qs.stringify(_data);
        _req._request(_url, 'POST',{"Content-Type":"application/x-www-form-urlencoded","Content-Length":_postData.length} , _postData  , function (data, result, response) {
            if (data !== null ){
                resolve(data.data);
            }
            else{
                reject(result);
            }
        });
    });
}